package com.doublefish.doublefishrobot.ui.home.path;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;
import com.doublefish.doublefishrobot.ui.home.path.bean.BallParamsBean;
import com.doublefish.doublefishrobot.ui.home.path.bean.VelocityBean;
import com.doublefish.doublefishrobot.ui.home.path.tools.PingPongTools;
import com.doublefish.doublefishrobot.ui.home.path.tools.PingPongUtils;
import com.doublefish.doublefishrobot.ui.home.path.tools.S5ParamTranslateUtils;
import com.doublefish.doublefishrobot.ui.home.path.tools.S6ParamTranslateUtils;
import com.doublefish.doublefishrobot.ui.home.path.tools.S8ParamTranslateUtils;
import com.doublefish.doublefishrobot.ui.home.path.view.PathXYView;
import com.doublefish.doublefishrobot.ui.home.path.view.PathYZView;

import java.util.List;

/**
 * 作者:dugaofeng on 2025/4/9 16:45
 */
public class DFPingPongTableView extends LinearLayout {

    private PathXYView xyView;
    private PathYZView yzView;



    public DFPingPongTableView(Context context) {
        this(context, null);
    }

    public DFPingPongTableView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DFPingPongTableView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public DFPingPongTableView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        View root = inflate(getContext(), R.layout.view_pingpong_table, this);
        xyView = root.findViewById(R.id.xy_view);
        yzView = root.findViewById(R.id.yz_view);
    }

    //超8 的发球轨迹
    public void setS8StepBean(ModeSettingBean.ModeSettingStepBean s8StepBean) {
        BallParamsBean bean = S8ParamTranslateUtils.s8ParamTranslate(s8StepBean);
        float elevation = PingPongTools.angleToRadian(bean.getElevation());
        float landPoint = PingPongTools.angleToRadian(bean.getLandPoint());
        float sideSpin = PingPongTools.angleToRadian(bean.getSideSpin());
        float rotation = bean.getRotation();
        //计算速度
        VelocityBean vel = PingPongTools.generateVelocityBean(bean.getSpeed(), elevation, landPoint);
        //乒乓球运行轨迹
        List<float[]> matrix = PingPongUtils.martix(bean.getHeight(), vel, rotation, sideSpin);
        xyView.setMatrix(matrix);
        yzView.setHeightAndMatrix(bean.getHeight(), matrix);
    }

    //超6 的发球轨迹
    public void setS6StepBean(ModeSettingBean.ModeSettingStepBean s6StepBean) {
        BallParamsBean bean = S6ParamTranslateUtils.s6ParamTranslate(s6StepBean);
        float elevation = PingPongTools.angleToRadian(bean.getElevation());
        float landPoint = PingPongTools.angleToRadian(bean.getLandPoint());
        float sideSpin = PingPongTools.angleToRadian(bean.getSideSpin());
        float rotation = bean.getRotation();
        //计算速度
        VelocityBean vel = PingPongTools.generateVelocityBean(bean.getSpeed(), elevation, landPoint);
        //乒乓球运行轨迹
        List<float[]> matrix = PingPongUtils.martix(bean.getHeight(), vel, rotation, sideSpin);
        xyView.setMatrix(matrix);
        yzView.setHeightAndMatrix(bean.getHeight(), matrix);
    }

    //超5 的发球轨迹
    public void setS5StepBean(ModeSettingBean.ModeSettingStepBean s5StepBean) {
        BallParamsBean bean = S5ParamTranslateUtils.s5ParamTranslate(s5StepBean);
        float elevation = PingPongTools.angleToRadian(bean.getElevation());
        float landPoint = PingPongTools.angleToRadian(bean.getLandPoint());
        float sideSpin = PingPongTools.angleToRadian(bean.getSideSpin());
        float rotation = bean.getRotation();
        //计算速度
        VelocityBean vel = PingPongTools.generateVelocityBean(bean.getSpeed(), elevation, landPoint);
        //乒乓球运行轨迹
        List<float[]> matrix = PingPongUtils.martix(bean.getHeight(), vel, rotation, sideSpin);
        xyView.setMatrix(matrix);
        yzView.setHeightAndMatrix(bean.getHeight(), matrix);
    }
}
